<template>
  <div class="login-wrap">
    <el-form class="login-container"
    :model="loginForm" :rules="rules" ref="loginForm">
      <h1 class="title">用户登录</h1>
      <el-form-item label="账号" prop="username">
        <el-input placeholder="请输入账号(默认身份证号)" autocomplete="true" v-model="loginForm.username"></el-input>
      </el-form-item>
      <el-form-item label="请输入密码" prop="password">
        <el-input type="password" placeholder="登录密码" v-model="loginForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width: 100%;" @click="confirm">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import $ from 'jquery'
import axios from "axios";

export default {
  name: 'login',
  data: function() {
    return {
      loginForm:{
        username:'',
        password:''
      },
      rules:{
        username: [
          { required: true, message: '请输入学号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输密码', trigger: 'blur' },
        ]
      }

    }
  },
  mounted(){
    // if(localStorage.getItem("studentName") !== null){
    //   window.location.href='/payInfo'
    // }
    axios.post('/api/app/login',this.loginForm).then(res=>{
      console.log(res.data)
      if(res.data.code==0){
        //存储token到本地
        //跳转到主页
        localStorage.setItem("studentName",res.data.data.name)
        localStorage.setItem("studentId",res.data.data.stuId)
        window.location.href='/payInfo'
      }else{
        return false;
      }
    });
  },
  methods: {
    change(){
      this.$forceUpdate();
    },

    confirm(){
      this.$refs.loginForm.validate((valid) => {
        if (valid) { //valid成功为true，失败为false
          //去后台验证用户名密码，并返回token
          axios.post('/api/app/login',this.loginForm).then(res=>{
            console.log(res.data)
            if(res.data.code==0){
              //存储token到本地
              //跳转到主页
              localStorage.setItem("studentName",res.data.data.name)
              localStorage.setItem("studentId",res.data.data.stuId)
              window.location.href='/payInfo'
            }else{
              alert('校验失败，用户名或密码错误！');
              return false;
            }
          });
        } else {
          console.log('校验失败');
          return false;
        }
      });
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 10%;
  background-image: url();
  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}

.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
</style>
